<template>

  <div v-wave="{
    color: '#fff',
    easing: 'ease-out',
    duration: 0.6,
    dissolveDuration: 0.25,
    initialOpacity: 0,

    finalOpacity: 0.1,
    cancellationPeriod: 75,
    trigger: 'auto',
    tagName: 'div'
  }" class="register">
    <el-container>
      <!-- <el-header class="header">

        <div class="re-top">
          <img src="../assets//images/logo38.png">
          <p class="p1">能力开放平台</p>
          <p class="p2">我已注册，现在就</p>
          <el-button type="primary" @click="gologin">登录</el-button>
        </div>
        <div class="foot-bj"></div>

      </el-header> -->
      <div class="login-import-item link-type" @click.prevent="toHome">返回首页</div>
      <div class="down" :class="gotolog">
        <div class="title-top">
          <p class="title">欢迎加入我们</p>
        </div>
        <div class="logbox">
          <div class="formbox">
            <el-form ref="registerForm" :model="registerForm" :rules="registerRules" class="register-form">
              <h3 class="title" style="font-size:18px;font-weight:500;color:white;">用户注册</h3>
              <!-- 用户名 -->
              <el-form-item prop="username" label-width="100px" label="用户名:">

                <el-input v-model="registerForm.username" type="text" auto-complete="off"
                  placeholder="5-15位,必须包含字母、数字和特殊字符,如:dc-^33名">
                  <!-- <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" /> -->
                </el-input>
              </el-form-item>
              <!-- 密码 -->
              <el-form-item prop="password" label-width="100px" label="密码:">
                <el-input v-model="registerForm.password" type="password" auto-complete="off"
                  placeholder="8-15位,必须包含字母、数字和特殊字符,如:dc-^33" @keyup.enter.native="handleRegister">
                  <!-- <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" /> -->
                </el-input>
              </el-form-item>
              <!-- 确认密码 -->
              <el-form-item prop="confirmPassword" label-width="100px" label="确认密码:">
                <el-input v-model="registerForm.confirmPassword" type="password" auto-complete="off" placeholder="确认密码"
                  @keyup.enter.native="handleRegister">
                  <!-- <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" /> -->
                </el-input>
              </el-form-item>
              <!-- 手机号 -->
              <el-form-item prop="phone" label-width="100px" label="手机号:">
                <div style="display:flex">
                  <!-- 下拉选择框 -->
                  <el-select v-model="phone" disabled>
                    <el-option selected>+86(中国)</el-option>
                  </el-select>
                  <el-input v-model="registerForm.phone" type="text" auto-complete="off" placeholder="请输入您的手机号">
                    <!-- <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" /> -->
                  </el-input>
                </div>
              </el-form-item>
              <!-- 验证码 -->
              <el-form-item prop="code" label-width="100px" label="验证码:">
                <div style="display: flex;width:470px">
                  <el-input style="width: 180px" v-model="registerForm.code" type="text" auto-complete="off">
                    <!-- <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" /> -->
                  </el-input>
                  <el-button class="verification">
                    免费获取短信验证码
                  </el-button>
                </div>
              </el-form-item>

              <el-form-item prop="birthday" label-width="100px" label="出生日期:">
                <div id="myDate">
                  <el-select v-model="yearsModel" @change="dateChange(1)" placeholder="请选择"
                    style="width:100px;margin-right:20px">
                    <el-option v-for="item in years" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                  </el-select>
                  <el-select v-model="monthsModel" @change="dateChange(2)" placeholder="请选择"
                    style="width:100px;margin-right:20px">
                    <el-option v-for="item in months" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                  </el-select>
                  <el-select v-model="daysModel" @change="dateChange(3)" placeholder="请选择"
                    style="width:100px;margin-right:20px">
                    <el-option v-for="item in days" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                  </el-select>
                </div>
              </el-form-item>

              <el-form-item prop="sex" label-width="100px" label="性别:">
                <div class="radio">
                  <el-radio v-model="registerForm.sex" label="1">男</el-radio>
                  <el-radio v-model="registerForm.sex" label="2">女</el-radio>
                </div>
              </el-form-item>



              <!-- <el-form-item prop="code" v-if="captchaEnabled">
              <el-input v-model="registerForm.code" auto-complete="off" placeholder="验证码" style="width: 63%"
                @keyup.enter.native="handleRegister">
                <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
              </el-input>
              <div class="register-code">
                <img :src="codeUrl" @click="getCode" class="register-code-img" />
              </div>
            </el-form-item> -->
              <el-form-item style="width:100%;">
                <el-button :loading="loading" size="medium" type="primary" style="width:100%;"
                  @click.native.prevent="handleRegister">
                  <span v-if="!loading">注 册</span>
                  <span v-else>注 册 中...</span>
                </el-button>

                <span class="tologin">已有账号？<span class="link-type" @click="gologin">立即登录</span></span>
                <!-- <div style="float: right;">
                <router-link class="link-type" :to="'/login'">使用已有账户登录</router-link>
              </div> -->
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>




    </el-container>





    <!--  底部  -->
    <div class="el-register-footer">
      <span>Copyright © 2018-2023 ruoyi.vip All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg, register } from "@/api/login";

export default {
  name: "Register",
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.registerForm.password !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    return {
      codeUrl: "",
      registerForm: {
        username: "",
        password: "",
        confirmPassword: "",
        monthsModel: "",
        daysModel: "",
        phone: "",
        code: "",
        yearsModel: "",
        uuid: "",
        sex: "",
      },
      yearsModel: null,
      years: [],
      monthsModel: null,
      months: [],
      daysModel: null,
      days: [],
      gotolog: '',
      radio: '1',

      registerRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
          { min: 5, max: 15, message: '用户账号长度必须介于 5 和 15 之间', trigger: 'blur' },
          {
            pattern: /(?![A-Z]*$)(?![a-z]*$)(?![0-9]*$)(?![^a-zA-Z0-9]*$)/,
            message: '必须包含字母、数字和特殊字符',
            trigger: 'blur'
          },
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
          { min: 5, max: 20, message: "用户密码长度必须介于 5 和 20 之间", trigger: "blur" },
          { pattern: /(?![A-Z]*$)(?![a-z]*$)(?![0-9]*$)(?![^a-zA-Z0-9]*$)/, message: "不能包含非法字符：< > \" ' \\\ |", trigger: "blur" }
        ],
        confirmPassword: [
          { required: true, trigger: "blur", message: "请再次输入您的密码" },
          { required: true, validator: equalToPassword, trigger: "blur" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }],
        phone: [{ required: true, trigger: "blur", message: "请输入手机号" }],
        // birthday: [{ required: true, trigger: "change", message: "请选择出生日期" }],
        sex: [{ required: true, trigger: "change", message: "请选择性别" }]

      },
      loading: false,
      captchaEnabled: true
    };
  },

  created() {
    this.getCode();
    this.init();
  },
  methods: {
    // 验证码
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.registerForm.uuid = res.uuid;
        }
      });
    },
    toHome() {
      this.$router.push("/home");
    },
    init() {
      var myDate = new Date;
      var year = myDate.getFullYear();//获取当前年
      var month = myDate.getMonth() + 1;//获取当前月
      var day = myDate.getDate();//获取当前日

      this.initSelectYear(year)
      this.initSelectMonth();
      this.initSelectDay(year, month);
      this.yearsModel = year;
      this.monthsModel = month;
      this.daysModel = day;
      let obj = { year: this.yearsModel, month: this.monthsModel, day: this.daysModel }
      this.$parent.dateChange(obj);
    },
    initSelectYear(year) {
      this.years = [];
      for (let i = 0; i < 30; i++) {
        this.years.push({ value: (year - i), label: (year - i) + "年" });
      }
    },
    initSelectMonth() {
      this.months = [];
      this.months.push({ value: 0, label: "全部" });
      for (let i = 1; i <= 12; i++) {
        this.months.push({ value: i, label: i + "月" });
      }
    },
    initSelectDay(year, month) {
      var maxDay = this.getMaxDay(year, month);
      this.days = [];
      this.days.push({ value: 0, label: "全部" });
      for (var i = 1; i <= maxDay; i++) {
        this.days.push({ value: i, label: i + "日" });
      }
    },
    dateChange(type) {
      //1年 2月 3日 4 左 5右
      if (type == 1 || type == 2) {
        if (this.monthsModel == 0) {
          this.daysModel = 0;
          this.initSelectDay(this.yearsModel, 1);
        } else {
          this.initSelectDay(this.yearsModel, this.monthsModel);
        }
      }
      if (type == 4) {
        this.dayleft();
      }
      if (type == 5) {
        this.dayright();
      }

      //操作父组件方法
      let obj = { year: this.yearsModel, month: this.monthsModel, day: this.daysModel }
      this.$parent.dateChange(obj);

    },
    getMaxDay(year, month) {
      var new_year = year;  //取当前的年份
      var new_month = month++;//取下一个月的第一天，方便计算（最后一天不固定）
      if (month > 12)      //如果当前大于12月，则年份转到下一年
      {
        new_month -= 12;    //月份减
        new_year++;      //年份增
      }
      var new_date = new Date(new_year, new_month, 1);//取当年当月中的第一天
      return (new Date(new_date.getTime() - 1000 * 60 * 60 * 24)).getDate();//获取当月最后一天日期
    },

    gologin() {
      this.gotolog = 'golog'
      setTimeout(() => {
        this.$router.push("/login");
      }, 700)

    },


    handleRegister() {
      // 注册按钮
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true;
          register(this.registerForm).then(res => {
            const username = this.registerForm.username;
            this.$alert("<font color='red'>恭喜你，您的账号 " + username + " 注册成功！</font>", '系统提示', {
              dangerouslyUseHTMLString: true,
              type: 'success'
            }).then(() => {
              this.$router.push("/login");
            }).catch(() => { });
          }).catch(() => {
            this.loading = false;
            if (this.captchaEnabled) {
              this.getCode();
            }
          })
        }
      });
    }
  },
  beforeRouteEnter(to, from, next) {
    console.log(from.fullPath, 'beforeRouteEnter');

    next(vm => {
      if (from.fullPath === '/login') {
        vm.gotolog = 'gotologre'
      }
      // vm相当于上面的this
      vm.fromPage = from.path;
    });

  },
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.register {
  position: relative;
  height: 100%;
  background: linear-gradient(rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0.7)), url(../assets/images/g2.jpg) no-repeat 0% 20%/ cover;
  background-size: cover;

  @keyframes logto {
    to {
      transform: rotate(90deg);
      opacity: 0;
    }

  }
  .golog {
 
    transform-origin: 200px 1500px;
    animation: logto 0.7s linear forwards;
  }

  @keyframes golog {
    to {
      transform: rotate(0deg);
      opacity: 1;
    }

  }

  .gotologre {
    transform: rotate(90deg);
    animation: golog 0.7s linear forwards;
    transform-origin: 150px 1200px;
    opacity: 0;

  }

  .down {
    margin: 120px auto;
    height: 600px;
    width: 600px;

    .title-top {
      height: 50px;
      position: relative;
      align-items: center;
      text-align: center;
      width: 600px;
      display: flex;


      .title {
        margin-left: 5px;
        // margin: 0px auto 100px auto;
        text-align: center;
        color: #ffffff8e;
        width: 600px;
        height: 50px;
        font-size: 35px;
        line-height: 50px;
        margin-bottom: 50px;
        word-spacing: 10px;
      }
    }

    .register-form {
      border-radius: 6px;
      background: rgb(34, 34, 36);
      width: 600px;
      padding: 25px 40px 5px 30px;
      text-align: left;

      .title {
        margin: 30px auto 30px auto;
        text-align: center;
        font-weight: 500;
        font-size: 14px;
      }

      ::v-deep .el-radio__inner {
        background: transparent;
        border: 1px solid rgb(30, 204, 152);
        color: rgb(30, 204, 152);
      }

      ::v-deep .el-radio__label {
        color: rgb(30, 204, 152);
      }

      ::v-deep .el-input {
        height: 38px;
        background: rgb(34, 34, 36);
        margin-bottom: 15px;
        border: 2px solid rgb(34, 34, 36);

        input:focus {
          border: 2px solid rgb(30, 204, 152);
          transition: 0.6s;
          border-radius: 10px;
          box-sizing: border-box;
        }

        input {
          height: 52px;
          background: rgb(34, 34, 36);
          border: 2px solid rgb(34, 34, 36);
          // border-radius: 10px;
          color: white;
          border-bottom: 2px solid rgb(30, 204, 152);
          transition: 0.6s;
          box-sizing: border-box;
        }

      }

      .el-button {
        background: rgb(30, 204, 152);
        border: 1px solid rgb(30, 204, 152);
        font-size: 18px;
      }

      .verification {
        width: 220px;
        margin-left: 20px;
        box-sizing: border-box;
        color: white;
      }

      .input-icon {
        height: 39px;
        width: 14px;
        margin-left: 2px;
      }
    }
  }
}

.el-form-item__label {
  width: 50px;
  text-align: left;

}

.radio {
  display: flex;
  width: 200px;
  padding: 10px;
}

.foot-bj {
  background-image: url("../assets/images/foot_bj2.png");
  height: 15px;
}

.login-import-item {
  position: absolute;
  top: 2%;
  left: 2%;
  color: white;
  font-size: 16px;
  z-index: 33;
}




.register-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.register-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-register-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.register-code-img {
  height: 38px;
}

.logbox::before {

  content: "";
  position: absolute;
  top: -50%;
  left: -50%;
  width: 612px;
  height: 712px;
  transform-origin: bottom right;
  animation: animate 6s linear infinite;
  background: linear-gradient(0deg, transparent, rgb(96, 253, 206), rgb(255, 255, 255));
}

.logbox {
  position: relative;
  width: 612px;
  height: 712px;
  overflow: hidden;
  border-radius: 6px;

  .formbox {
    position: absolute;
    inset: 3px;
    background: rgb(34, 34, 36);
    z-index: 10;
    border-radius: 6px;
    border: 3px solid #000000;
  }
}

.logbox::after {

  content: "";
  position: absolute;
  top: -50%;
  left: -50%;
  width: 612px;
  height: 712px;
  transform-origin: bottom right;
  animation: animate 6s linear infinite;
  animation-delay: -3s;
  background: linear-gradient(0deg, transparent, rgb(94, 238, 195), rgb(255, 255, 255));
}

@keyframes animate {
  0% {
    transform: rotate(0deg);
  }

  100% {
    transform: rotate(360deg);
  }
}

.tologin {
  width: 150px;
  float: right;
  height: 15px;

}

.link-type {
  color: rgb(30, 204, 152);
}
</style>
